#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
int n, m;
struct g
{
    int a, b, w;
}edgs[N];
int p[N];
int Find(int x)
{
    if (p[x] != x) p[x] = Find(p[x]);
    return p[x];
}
bool cmp(g& g1, g& g2)
{
    return g1.w < g2.w;
}
int Kruskal()
{
    sort(edgs, edgs + m, cmp);
    for (int i = 1; i <= n; i++) p[i] = i;
    int ans = 0;
    int cnt = 0;
    for (int i = 0; i < m; i++)
    {
        int a = edgs[i].a, b = edgs[i].b, w = edgs[i].w;
        if (Find(a) != Find(b))
        {
            p[Find(a)] = Find(b);
            ans += w;
            cnt++;
        }
    }
    if (cnt != n - 1) return 0x3f3f3f3f;
    else return ans;
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        edgs[i] = { a, b, c };
    }
    int ans = Kruskal();
    if (ans > 0x3f3f3f3f / 2) cout << "impossible" << endl;
    else cout << ans;
    return 0;
}